package com.practice.java8.stream;

import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class GenerateMain {

	public static void main(String[] args) {
		generate();
		supplier();
		iterate();
	}
	
	public static void generate() {
		// By Random
		Random seed = new Random();
		Supplier<Integer> random = seed::nextInt;
		Stream.generate(random).
			limit(10).forEach(System.out::println);
		// By time
		IntStream.generate(() -> (int) (System.nanoTime() % 100)).
			limit(10).forEach(System.out::println);
	}
	
	public static void supplier() {
		Stream.generate(new PersonSupplier()).
		limit(10).
		forEach(p -> System.out.println(p.getNumber() + ", " + p.getAge()));
	}
	
	public static void iterate() {
		Stream.iterate(0, n -> n + 3).limit(10). forEach(x -> System.out.print(x + " "));
	}

	private static class PersonSupplier implements Supplier<Person> {

		private int index = 0;

		private Random random = new Random();

		@Override
		public Person get() {
			return new Person(index++, random.nextInt(100));
		}

	}
	
	private static class Person {

		private Integer number;

		private Integer age;
		
		public Person(Integer number, Integer age) {
			this.number = number;
			this.age = age;
			System.out.println(number + ", " + age);
		}

		public Integer getNumber() {
			return number;
		}

		public Integer getAge() {
			return age;
		}

	}

}
